﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Response
{
    /// <summary>
    /// 响应消息
    /// </summary>
    public class ResponseContext
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        protected const int ErrorCode = 500;

        /// <summary>
        /// 成功代码
        /// </summary>
        protected const int SuccessCode = 200;

        /// <summary>
        /// 响应消息
        /// </summary>
        public ResponseContext()
        {
            Code = 0;
            Success = false;
            Msg = string.Empty;
        }

        /// <summary>
        /// 返回状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 返回消息
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        /// 成功标识
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 设置成功
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="success">成功标识</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public ResponseContext SetSuccess(string msg = "操作成功", bool success = true, int code = 200)
        {
            return new ResponseContext()
            {
                Success = success,
                Msg = msg,
                Code = code
            };
        }

        /// <summary>
        /// 设置失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="success">成功标识</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public ResponseContext SetError(string msg = "操作失败", bool success = false, int code = ErrorCode)
        {
            return new ResponseContext()
            {
                Success = success,
                Msg = msg,
                Code = code
            };
        }
    }

    /// <summary>
    /// 响应消息和数据
    /// </summary>
    public class ResponseContext<TData> : ResponseContext where TData : class
    {
        /// <summary>
        /// 响应消息和数据
        /// </summary>
        public ResponseContext()
        {
            Code = 0;
            Success = false;
            Msg = string.Empty;
            Data = default;
        }

        /// <summary>
        /// 响应数据
        /// </summary>
        public TData Data { get; set; }

        /// <summary>
        /// 设置成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="msg">消息</param>
        /// <param name="success">成功标识</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public ResponseContext<TData> SetSuccess(TData data, string msg = "操作成功", bool success = true, int code = SuccessCode)
        {
            return new ResponseContext<TData>()
            {
                Success = success,
                Msg = msg,
                Code = code,
                Data = data
            };
        }

        /// <summary>
        /// 设置失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="success">成功标识</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public new ResponseContext<TData> SetError(string msg = "操作失败", bool success = false, int code = ErrorCode)
        {
            return new ResponseContext<TData>()
            {
                Success = success,
                Msg = msg,
                Code = code,
                Data = default(TData)
            };
        }
    }

    /// <summary>
    /// 分页查询返回数据
    /// </summary>
    public class ResponsePageContext<TData> : ResponseContext where TData : class
    {
        /// <summary>
        /// 分页查询返回数据
        /// </summary>
        public ResponsePageContext()
        {
            Code = 0;
            Success = false;
            Msg = string.Empty;
            Data = new PageInfo<TData>(0, null);
        }

        /// <summary>
        /// 数据状态
        /// </summary>
        public PageInfo<TData> Data { get; set; }

        /// <summary>
        /// 设置成功
        /// </summary>
        /// <param name="count">总数据条数</param>
        /// <param name="rows">数据</param>
        /// <param name="msg">消息</param>
        /// <param name="success">成功标识</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public ResponsePageContext<TData> SetSuccess(long count, List<TData> rows, string msg = "操作成功", bool success = true, int code = SuccessCode)
        {
            return new ResponsePageContext<TData>()
            {
                Success = success,
                Msg = msg,
                Code = code,
                Data = new PageInfo<TData>(count, rows)
            };
        }

        /// <summary>
        /// 设置失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="success">成功标识</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public new ResponsePageContext<TData> SetError(string msg = "操作失败", bool success = false, int code = ErrorCode)
        {
            return new ResponsePageContext<TData>()
            {
                Success = success,
                Msg = msg,
                Code = code,
                Data = new PageInfo<TData>(0, null)
            };
        }
    }

    /// <summary>
    /// 分页的每页数据
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class PageInfo<TData> where TData : class
    {
        /// <summary>
        /// 分页的每页数据
        /// </summary>
        public PageInfo()
        {
            count = 0;
            rows = new List<TData>();
        }

        /// <summary>
        /// 分页初始值
        /// </summary>
        /// <param name="count">总数据条数</param>
        /// <param name="rows">当前页数据</param>
        public PageInfo(long count, List<TData> rows)
        {
            this.count = count;
            this.rows = rows;
        }

        /// <summary>
        /// 总数据条数
        /// </summary>
        public long count { get; set; }

        /// <summary>
        /// 每页数据
        /// </summary>
        public List<TData> rows { get; set; }
    }
}